<template>
  <!-- 放置一个图标 -->
  <div class="screenWrapper">
    <svg-icon
      :icon-class="isFullScreen? 'exit-fullscreen': 'fullscreen'"
      class="fullscreen"
      @click="toggleScreen"
    />
  </div>
</template>

<script>
import screenfull from 'screenfull'
export default {
  name: 'ScreenFull',
  data() {
    return {
      isFullScreen: false
    }
  },
  mounted() {
    screenfull.on('change', () => {
      // 把自己用来控制图标显示的isFullScreen 和人家给的保持相同
      this.isFullScreen = screenfull.isFullscreen
    })
  },
  methods: {
    toggleScreen() {
      // 点击按钮切换全屏
      screenfull.toggle()
    }
  }
  // mounted() {
  //   window.addEventListener('fullscreenchange', () => {
  //     // 监听到屏幕变化，在回调中判断是否已退出全屏 如果已退出全屏 把本地状态修改为false
  //     const isFull = document.fullscreenElement
  //     if (isFull == null) {
  //       this.isFullScreen = false
  //     }
  //   })
  // },
  // methods: {
  //   toggleScreen() {
  //     // 全屏切换  取反操作
  //     if (this.isFullScreen) {
  //       // 退出全屏
  //       document.exitFullscreen()
  //     } else {
  //       // 开启全屏
  //       document.documentElement.requestFullscreen()
  //     }
  //     this.isFullScreen = !this.isFullScreen
  //   }
  // }
}
</script>

<style lang="scss" scoped>
.screenWrapper {
  display: flex;
  align-items: center;
  margin-right: 20px;
}
.fullscreen {
  width: 20px;
  height: 20px;
  color: #fff;
  cursor: pointer;
}
</style>
